<?php


namespace app\common\business;


use app\common\lib\pay\Weixin;
use think\facade\Cache;

class Pay
{
    public function unifiedOrder($payType, $data) {
        if ($payType == "weixin") {
            $res = Weixin::unifiedOrder($data);
        }

        if ($payType == "zhifubao") {
            $res = $this->aliPay($data);
        }

        //把支付状态放入redis
        $orderData = [
            'pay_status'=> 0,
            'pay_type'=> $payType,
            'create_time'=> time()
        ];
        //放入redis
        try {
            Cache::store('redis')->hset(config('pay.pay_status_key'), $data['order_id'], json_encode($orderData));
        }catch (\Exception $e) {
            echo $e->getMessage();exit;
            return false;
        }

        return ['code_url'=> $res];
    }

    /**
     * 微信支付
     * @param $data
     */
    public function weixinPay($data) {
        $notify = new WxPayNativePay();
        $input = new WxPayUnifiedOrder();
        $input->SetBody(config('pay.pay_title'));
        $input->SetAttach($data['title']);
        $input->SetOut_trade_no("singwa".date("YmdHis"));
        $input->SetTotal_fee($data['total_price']);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 300));
        $input->SetGoods_tag($data['title']);
        $input->SetNotify_url("https://pay.singwa666.com/pay/notify/weixin");
        $input->SetTrade_type("NATIVE");
        $input->SetProduct_id($data['goods_id']);

        $result = $notify->GetPayUrl($input);
        $url2 = $result["code_url"];
        echo "<img src='".url("pay/qrCode", ["data"=>$url2])."'>";
    }


    /**
     * 支付宝支付
     * @param $data
     */
    public function aliPay($data) {

    }
}